<?php

if (!defined('IN_DISCUZ')){
	exit('Access Denied');
}

global $_G;

$uData = $profile = $gp = $post_erro = $product = array();
$submited = false;
define('NOROBOT', true);
$seccodecheck = 1;
$postJS = false;
$profileJS = false;

$actionArray = array(
		'profile',
		'post',
		'estore',
		'manage',
		'register',
		'mylist',
		'notice',
		'showcart',
		'mybill'
);

$ac = isset($_G['gp_ac']) ? strval($_G['gp_ac']) : '';
$do = isset($_G['gp_do']) ? strval($_G['gp_do']) : '';
$uid = isset($_G['gp_uid']) ? intval($_G['gp_uid']) : 0;
$sid = isset($_G['gp_sid']) ? intval($_G['gp_sid']) : 0;

if (!in_array($ac, $actionArray)){
	showmessage('undefined_action', NULL);
}

if (!$_G['uid']){
	showmessage('postperm_login_nopermission', NULL, array(), array(
			'login'=>1
	));
}elseif (($_G['adminid'] == 1 && $uid) || ($ac == 'profile' && $do == 'update' && $_G['uid'])){

	if ($_G['adminid'] == 1){
		$userID = $uid;
	}else{
		$userID = $_G['uid'];
	}

	$user = DB::fetch_first("SELECT c.uid, c.username
							 FROM " . DB::table("common_member") . " c
							 WHERE c.uid = " . $userID);
	$uData = check_profile_shop($user['uid']);
}else
	$uData = check_profile_shop($_G['uid']);

// Danh sach Admin user
$NoteToAdmin = array(2,10);
$NoteToAdmin = array_unique($NoteToAdmin);

// Right menu
$store_menu = array(
		lang('shop/template', 'cap_nhat_gian_hang')=>'shop.php?mod=store&ac=profile&do=update',
		lang('shop/template', 'dang_san_pham')=>'shop.php?mod=store&ac=post&do=new',
		lang('shop/template', 'danh_sach_san_pham')=>'shop.php?mod=store&ac=mylist',
		lang('shop/template', 'danh_sach_don_hang')=>'shop.php?mod=store&ac=mybill&do=sale',
		lang('shop/template', 'da_mua')=>'shop.php?mod=store&ac=mybill&do=buy'
);

switch ($ac){
	case 'register':
		$navtitle = $metakeywords = $metadescription = $profile_title = lang('shop/template', 'register_shoper');

		if ($_G['adminid'] == 1)
			showmessage('ban_khong_duoc_phep_mo_gian_hang');

		if ($do == 'agree'){
			$profileJS = true;

			$sCat = array();
			foreach ($_G['cache']['shops'] as $key=>$value){
				if ($value['type'] == 'group'){
					$sCat[$value['sid']] = $value['name'];
				}
			}

			require libfile('shop/reg_store', 'include');
		}else
			include template('shop/store/begin');
	break;
	case 'profile':
		if ($do == 'update'){
			$profileJS = true;
			$navtitle = $metakeywords = $metadescription = $profile_title = lang('shop/template', 'update_shop_profile');
			for ($i = 0; $i <= 10; $i++){
				$stars[] = $i;
			}

			$sCat = array();
			foreach ($_G['cache']['shops'] as $key=>$value){
				if ($value['type'] == 'group'){
					$sCat[$value['sid']] = $value['name'];
				}
			}

			require libfile('shop/update_store', 'include');
		}
	break;
	case 'mylist':
		$navtitle = $metakeywords = $metadescription = $profile_title = lang('shop/template', 'danh_sach_san_pham');
		require libfile('shop/my_products', 'include');
		include template('shop/store/my_products');
	break;
	case 'post':
		$postJS = true;
		$pid = isset($_G['gp_pid']) ? intval($_G['gp_pid']) : 0;

		// Thoi gian dang san pham
		if ($do == 'new')
			$product['posttime'] = $product['lastupdate'] = TIMESTAMP;
		else
			$product['lastupdate'] = TIMESTAMP;

		// Cac tinh/thanh cua VietNam
		$provice = provice();
		$product['location_province'] = $uData['store_province'];

		/*
		 * Don vi tinh
		 * */
		$unit = array(
				lang('shop/template', 'unit_chiec'),
				lang('shop/template', 'unit_bo'),
				lang('shop/template', 'unit_hop'),
				lang('shop/template', 'unit_thung'),
				lang('shop/template', 'unit_doi'),
				lang('shop/template', 'unit_kg'),
				lang('shop/template', 'unit_ta'),
				lang('shop/template', 'unit_luong'),
				lang('shop/template', 'unit_lit'),
				lang('shop/template', 'unit_met'),
				lang('shop/template', 'unit_met_2'),
				lang('shop/template', 'unit_met_3')
		);

		/*
		 * Tinh trang san pham
		 * trong kho hang
		 * */
		$product['status'] = 1;
		$store_status = array(
				1=>lang('shop/template', 's_con_hang'),
				2=>lang('shop/template', 's_dang_ve'),
				3=>lang('shop/template', 's_dang_dat_hang'),
				0=>lang('shop/template', 's_het_hang')
		);

		/*
	 * Upload images
	 * */
		$_G['shop']['allowpostattach'] = isset($_G['shop']['allowpostattach']) ? $_G['shop']['allowpostattach'] : '0';
		$_G['shop']['allowpostimage'] = isset($_G['shop']['allowpostimage']) ? $_G['shop']['allowpostimage'] : '0';
		$_G['group']['allowpostattach'] = $_G['shop']['allowpostattach'] != -1 && ($_G['shop']['allowpostattach'] == 1 || (!$_G['shop']['postattachperm'] && $_G['group']['allowpostattach']) || ($_G['shop']['postattachperm'] && forumperm($_G['shop']['postattachperm'])));
		$_G['group']['allowpostimage'] = $_G['shop']['allowpostimage'] != -1 && ($_G['shop']['allowpostimage'] == 1 || (!$_G['shop']['postimageperm'] && $_G['group']['allowpostimage']) || ($_G['shop']['postimageperm'] && forumperm($_G['shop']['postimageperm'])));
		$_G['group']['attachextensions'] = $_G['shop']['attachextensions'] ? $_G['shop']['attachextensions'] : $_G['group']['attachextensions'];

		if ($_G['group']['attachextensions']){
			$imgexts = explode(',', str_replace(' ', '', $_G['group']['attachextensions']));
			$imgexts = array_intersect(array(
					'jpg',
					'jpeg',
					'png',
					'bmp'
			), $imgexts);
			$imgexts = implode(', ', $imgexts);
		}else{
			$imgexts = 'jpg, jpeg, png, bmp';
		}
		// allow post max attach of one day
		$allowuploadnum = $allowuploadtoday = TRUE;
		if ($_G['group']['allowpostattach'] || $_G['group']['allowpostimage']){
			if ($_G['group']['maxattachnum']){
				$allowuploadnum = $_G['group']['maxattachnum'] - getuserprofile('todayattachs');
				$allowuploadnum = $allowuploadnum < 0 ? 0 : $allowuploadnum;
				if (!$allowuploadnum){
					$allowuploadtoday = false;
				}
			}
			if ($_G['group']['maxsizeperday']){
				$allowuploadsize = $_G['group']['maxsizeperday'] - getuserprofile('todayattachsize');
				$allowuploadsize = $allowuploadsize < 0 ? 0 : $allowuploadsize;
				if (!$allowuploadsize){
					$allowuploadtoday = false;
				}
				$allowuploadsize = $allowuploadsize / 1048576 >= 1 ? round(($allowuploadsize / 1048576), 1) . 'MB' : round(($allowuploadsize / 1024)) . 'KB';
			}

			if ($allowuploadsize == 0)
				$allowuploadsize = lang('shop/template', 'khong_gio_hang');
		}

		// Property Post IMG
		$allowpostimg = $_G['group']['allowpostimage'] && $imgexts;
		$enctype = ($_G['group']['allowpostattach'] || $_G['group']['allowpostimage']) ? 'enctype="multipart/form-data"' : '';
		$maxattachsize_mb = $_G['group']['maxattachsize'] / 1048576 >= 1 ? round(($_G['group']['maxattachsize'] / 1048576), 1) . 'MB' : round(($_G['group']['maxattachsize'] / 1024)) . 'KB';

		/*
		 * ***************************************************
		 * Set up editor
		 * ***************************************************/
		$editorid = 'e'; // dat ID cho bo soan thao.
		$_G['setting']['editoroptions'] = str_pad(decbin($_G['setting']['editoroptions']), 3, 0, STR_PAD_LEFT);

		$editormode = $_G['setting']['editoroptions']{0}; // gia su $_G['setting']['editoroptions'] = 'abcd' => $_G['setting']['editoroptions']{0} = 'a'
		$allowswitcheditor = $_G['setting']['editoroptions']{1}; // gia su $_G['setting']['editoroptions'] = 'abcd' => $_G['setting']['editoroptions']{1} = 'b'
		/*
		 * y o day la chung ta tach doat chuoi cai dat cua $_G['setting']['editoroptions'], voi moi vi tri tuong ung se dua ra mot gia tri cai dat
		 * **/
		$editor = array(
				'editormode'=>$editormode,
				'allowswitcheditor'=>$allowswitcheditor,
				'allowhtml'=>1,  //$_G['shop']['allowhtml'] Tuy tung noi cho phep cai dat, co the viet them trong admin de cau hinh
				'allowsmilies'=>1,  //$_G['shop']['allowsmilies'] Tuy tung noi cho phep cai dat, co the viet them trong admin de cau hinh
				'allowbbcode'=>1,  //$_G['shop']['allowbbcode'] Tuy tung noi cho phep cai dat, co the viet them trong admin de cau hinh
				'allowimgcode'=>1,  //$_G['shop']['allowimgcode'] Tuy tung noi cho phep cai dat, co the viet them trong admin de cau hinh
				'allowresize'=>1,
				'allowchecklength'=>1,
				'allowtopicreset'=>0,
				'textarea'=>'detail',  // Dat ten cho hop textarea
				'simplemode'=>!isset($_G['cookie']['editormode_' . $editorid]) ? !$_G['setting']['editoroptions']{9} : $_G['cookie']['editormode_' . $editorid],
				'value'=>'' // gia tri dua vao hop textarea de edit neu trong truong hop edit
		);

		$_G['forum']['allowmediacode'] = true; //set de hop soan thao cho phep dang media
		$_G['group']['allowmediacode'] = true; // set de cap quyen cho nhom nay dc phep dung code media
		$_G['group']['allowpostattach'] = FALSE; //  set de cho phep hay khong viec su dung attachment


		$notAllowInsertIMGFromAlbum = true;
		$isfirstpost = true; // Bai viet dau tien


		/*******************************************************************************************************/
		// san pham moi 100%
		$product['state'] = '100%';
		// IP Client
		$product['clientIP'] = $_G['clientip'];

		if ($do == 'new'){
			$navtitle = $metakeywords = $metadescription = $profile_title = lang('shop/template', 'them_san_pham_moi');

			require libfile('shop/product_new', 'include');
		}elseif ($do == 'edit'){
			$navtitle = $metakeywords = $metadescription = $profile_title = lang('shop/template', 'chinh_sua_pham');

			require libfile('shop/product_edit', 'include');
		}elseif ($do == 'delete')
			require libfile('shop/del_product', 'include');
	break;
	case 'notice':
		$navtitle = $metakeywords = $metadescription = $profile_title = lang('shop/template', 'gui_thong_tin_toi_cac_gian_hang');
		require libfile('shop/notice', 'include');
		include template('shop/store/notice');
	break;
	case 'showcart':
		$navtitle = $metakeywords = $metadescription = $profile_title = lang('shop/template', 'gio_hang_cua_ban');

		require libfile('shop/showcart', 'include');
		include template('shop/detail/showcart');
	break;
	case 'mybill':
		$navtitle = $metakeywords = $metadescription = $profile_title = lang('shop/template', 'danh_sach_don_hang');

		require libfile('shop/mybill', 'include');
		include template('shop/store/mybill');
	break;
	default:
		require libfile('shop/manager_store', 'include');
		include template('shop/store/manager_store');
	break;
}